package lib.bone.access.module.dict.impl;


import cn.hutool.core.lang.Validator;
import lib.bone.access.constant.ResearchTypeConst;
import lib.bone.access.module.dict.dao.NepuPaperDictDAO;
import lib.bone.access.module.dict.entity.NepuPaperDict;
import lib.bone.access.module.dict.DictService;
import lib.bone.common.constant.ResponseCodeConst;
import lib.bone.common.core.domain.PageResultDTO;
import lib.bone.common.core.domain.ResponseDTO;
import lib.bone.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author  :贾斯淇
 */

@Slf4j
@Service
public class PaperDictServiceImpl implements DictService<NepuPaperDict,String> {
    @Autowired
    NepuPaperDictDAO nepuPaperDictDAO;

    public List<String> getDictIdsByType(String type){
        List<NepuPaperDict> dicts = nepuPaperDictDAO.findByType(type);
        return dicts.stream().map(NepuPaperDict::getId).collect(Collectors.toList());
    }

//    @Autowired
//    NepuPaperDAO nepuPaperDAO;

    @Override
    public PageResultDTO<NepuPaperDict> getAll(Pageable pageable, Example<NepuPaperDict> example) {

        Page<NepuPaperDict> nepuPaperDictPage;
        //是否为空 --
        if(Validator.isEmpty(example)){
            nepuPaperDictPage = nepuPaperDictDAO.findAll(pageable);
        }else {
            nepuPaperDictPage = nepuPaperDictDAO.findAll(example,pageable);
        }
        List<NepuPaperDict> nepuPaperDictList = nepuPaperDictPage.getContent();
        PageResultDTO<NepuPaperDict> pageResultDTO = new PageResultDTO<>();
        pageResultDTO.setContent(nepuPaperDictList);
        pageResultDTO.setSize(nepuPaperDictPage.getSize());
        pageResultDTO.setTotalPages(nepuPaperDictPage.getTotalPages());
        pageResultDTO.setNumber(nepuPaperDictPage.getNumber());
        pageResultDTO.setTotalElements(nepuPaperDictPage.getTotalElements());

        return pageResultDTO;
    }

    @Override
    public ResponseDTO deleteById(String id) {
        /**
         * 1. 是否有使用到该词典数据的内容
         */
//        Long useCount = nepuPaperDAO.countByPaperDictId(id);
//        if(useCount > 0L){
//            return ResponseDTO.wrap(ResponseCodeConst.FORBIDDEN,"存在关联该词典的论文信息,拒绝执行删除!");
//        }
        // 2. 判断资源内容是否存在
        Optional<NepuPaperDict> nepuPaperDictOp = nepuPaperDictDAO.findById(id);
        if(!nepuPaperDictOp.isPresent()) return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND,"未找到ID:["+id+"]对应资源!");
        nepuPaperDictDAO.deleteById(id);
        return ResponseDTO.succMsg("删除成功!");
    }

    @Override
    public Optional<NepuPaperDict> getById(String id) {
        return nepuPaperDictDAO.findById(id);
    }

    @Override
    public NepuPaperDict saveOne(NepuPaperDict nepuPaperDict) {
        if (Validator.isNotEmpty(nepuPaperDict.getId())){
            throw new BusinessException(ResponseCodeConst.ERROR_PARAM);
        }
        return nepuPaperDictDAO.save(nepuPaperDict);
    }

    @Override
    public NepuPaperDict updateOne(NepuPaperDict nepuPaperDict) {
        if (Validator.isEmpty(nepuPaperDict.getId())){
            throw new BusinessException(ResponseCodeConst.ERROR_PARAM);
        }
        return nepuPaperDictDAO.save(nepuPaperDict);
    }

    @Override
    public String getServiceType() {
        return ResearchTypeConst.PAPER;
    }
}
